/*
 * CVideoRender.cpp
 *
 *  Created on: 2017年3月16日
 *      Author: chuanjiang.zh
 */

#include "CVideoRender.h"
#include <errno.h>
#include "TimeHelper.h"
#include "RenderConst.h"
#include "CLog.h"
#include <iostream>
#include <stdlib.h>
#include "FfmpegUtil.h"

namespace av
{

CVideoRender::CVideoRender():
		m_state(STATE_STOPPED)
{
}

CVideoRender::~CVideoRender()
{
	close();

	destroy();
}

int CVideoRender::create(void* hwnd)
{
	return m_window.open(hwnd);
}

void CVideoRender::destroy()
{
	m_window.close();
}

int CVideoRender::open(const MediaFormat& fmt)
{
	return 0;
}

void CVideoRender::close()
{
}


bool CVideoRender::isOpen()
{
	return m_window.isOpen();
}


int CVideoRender::play()
{
	setState(STATE_PLAYING);

	return 0;
}

int CVideoRender::pause()
{
	setState(STATE_PAUSED);

	return 0;
}

void CVideoRender::stop()
{
	setState(STATE_STOPPED);

}


int CVideoRender::getState()
{
	comn::AutoCritSec lock(m_cs);
	return m_state;
}


int CVideoRender::write(AVFrame* frame)
{
	if (!frame)
	{
		return EINVAL;
	}

	AVFramePtr frameptr = ffmpeg::makeFrame(frame);
	m_window.draw(frameptr);

	return 0;
}


void CVideoRender::flush()
{
	m_window.flush();
}


int CVideoRender::setRatio(int width, int height)
{
	m_window.setRatio(width, height);
	return 0;
}


void CVideoRender::setVisible(bool visible)
{
	m_window.setVisible(visible);
}

void CVideoRender::setState(StreamState state)
{
	m_window.setState(state);

	comn::AutoCritSec lock(m_cs);
	m_state = state;
}


} /* namespace av */
